function gadget:GetInfo()
	return {
		name = "King of the Hill Countdown",
		desc = "Tracks a player's total flag-control time until it reaches the goal time",
		author = "Max Lerner, Roy Wiggins, Nemo (B. Tyler), FLOZi",
		date = "2010-11-09",
		license = "Public domain",
		layer = 0,
		enabled = true
	}
end


-- function localisations
-- Synced Read
local GetUnitDefID = Spring.GetUnitDefID
local GetUnitRulesParam	= Spring.GetUnitRulesParam
local GetUnitTeam = Spring.GetUnitTeam
-- Synced Ctrl
local SetUnitRulesParam	= Spring.SetUnitRulesParam

-- constants
local FLAGSECS_TO_WIN = 180	--THIS IS WHERE WE SET THE TARGET NUMBER OF SECONDS TO WIN!!!!!----
local GAIA_TEAM_ID		= Spring.GetGaiaTeamID()


local teamCtrlTime = {}
if (gadgetHandler:IsSyncedCode()) then

--SYNCED

local function popTeamTimeTable()
	--Initializes each team's flagsecs of control to 0
	--Dictionary of teamID => time
	for i, teamID in pairs(Spring.GetTeamList()) do
		if teamID ~= GAIA_TEAM_ID then
			teamCtrlTime[teamID] = 0
		end
	end
end
popTeamTimeTable()

function gadget:GameFrame(t)
	if GG['donePicking'] == true then
		if t == 6 then
			oldflags = GG['flags']
			if GG['flags'][1]~=nil then
				local flagDefID = GetUnitDefID(GG['flags'][1])
				local flagUD = UnitDefs[flagDefID]

			end

		end
		
		if (t % (30) < 0.1) and t > 6 then	-- every second
			if GG['flags'][1]~=nil then
				for i = 1, #GG['flags'] do
					flagID = GG['flags'][i]
					teamID = GetUnitTeam(flagID)
					if teamID ~= GAIA_TEAM_ID and teamID ~= nil and teamCtrlTime[teamID] ~= nil then -- Neutral flags do not work towards win cond
						teamCtrlTime[teamID] = teamCtrlTime[teamID] + 1
						
					end
				end
				for teamID, score in pairs(teamCtrlTime) do
				
					--IF A PLAYER HAS WON:
					if score > FLAGSECS_TO_WIN then
						local winner = teamID
						if teamID == GG['koth'][1] or teamID == GG['koth'][2] then
							Spring.Echo("VICTORY FOR PLAYER", teamID)
							GG.gameOver(teamID, -1)
							gadgetHandler:RemoveGadget()
						end					
					end
				end		
			end
			oldflags = GG['flags']
		end
		
		--[[if (t % (30*5) < 0.1) and t > 6 then --Every 5 seconds
			for teamID, score in pairs(teamCtrlTime) do
				Spring.Echo("TeamID " .. teamID .. "	SCORE " .. score)
			end
		end]]--

		if (t % (1*30) < 0.1) then 
			if GG['koth'][1] == -1 and GG['koth'][2] == -1 and GG['donePicking'] == true then
				--Spring.Echo("removing KOTH gadget")
				gadgetHandler:RemoveGadget()
			end
		end
	end
end

else
--UNSYNCeED
end
